﻿<?php
if(!defined('IN_PHPSCUP'))exit('hacking'); 

class IntegralApp extends BaseApp
{
    var $type = 'integral';
    function index()
    {
        if(!isset($_GET['roleid']))
		{
		$_GET['roleid'] = '';
		}	

        if(!isset($_GET['fieldtype']))
		{
		$_GET['fieldtype'] = '';
		}					
        if(!isset($_POST['fieldtype']))
		{
		$_POST['fieldtype'] = '';
		}		
		if($_GET['fieldtype'])
		{
	    $fieldtype = $_GET['fieldtype'];		
		}
		if($_POST['fieldtype'])
		{
	    $fieldtype = $_POST['fieldtype'];		
		}		
					
		
		
        if(!isset($_GET['word']))
		{
		$_GET['word'] = '';
		}					
        if(!isset($_POST['word']))
		{
		$_POST['word'] = '';
		}
		if($_GET['word'])
		{
	    $word = $_GET['word'];		
		}
		if($_POST['word'])
		{
	    $word = $_POST['word'];		
		}
		$word = trim($word)	;	
		$roleid = $_GET['roleid'];
		$word_array = explode(' ',$word);
		
		foreach($word_array as $key => $value)
		{
			if(!$value)
			{
             unset($word_array[$key]);
		    }  
		}		

		$TB_role = &$this->_model('role');			
		$roledata_temp = $TB_role->selectshow();
        $roleicon = role_con(ROLE_CON_FILE);
		$roledata = array();
		$rolename = array();
		foreach($roledata_temp as $key => $value)
		{
		    $str = '';
			if(isset($value['role_name']))
			{
			     foreach($roleicon as $key1 => $value1)
				 {
				     $str .= $value1.':'.$value['role_id'].'|';
				 }
			     
				 $roledata[$value['role_id']]['name'] = $value['role_name'];
				 $roledata[$value['role_id']]['value'] = substr($str, 0, -1);
				 $rolename[$roledata[$value['role_id']]['value']] = $value['role_name'];
			}
		}
        $this->assign('rolename', $rolename);			
        $this->assign('roledata', $roledata);		
        $this->assign('roleicon', $roleicon);	

		$this->assign('word', $word);
		$this->assign('roleid', $roleid);
		$this->assign('fieldtype', $fieldtype);
		
						
		$TB_integral = &$this->_model('integral');  
		
		$data = $TB_integral->allshow($word_array,$word,$roleid,$fieldtype);
				
        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);		
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('integral_list.view.php');
    }



    function addpost()
    {

	     $type = $_POST['type'];
		 
	     $username = $_POST['username'];
	     $jiajian = $_POST['jiajian'];		 		 
	     $jifeng1 = $_POST['jifeng1'];
		 
	     $huiyuantype = $_POST['huiyuantype'];			 
	     $jifeng2 = $_POST['jifeng2'];
	     $content = $_POST['content'];
         if(!preg_match("/^[0-9]*$/", $jifeng1))
		 {
	          $notice = &get_message(array('msg'=>'积分只能输入数字！','url'=>'?app=integral','title'=>'Notice'));
	          $notice ->display();			 
		 }		 
         if(!preg_match("/^[0-9]*$/", $jifeng2))
		 {
	          $notice = &get_message(array('msg'=>'积分只能输入数字！','url'=>'?app=integral','title'=>'Notice'));
	          $notice ->display();			 
		 }		 

	     $TB_integral = &$this->_model('integral');		 		 
         if($type == 1)
		 {
	          $user = & $GLOBALS['SCup']->_get_user();
		      $tempdata = $user->get_info($username);
			  $totaljifeng =$tempdata['integral'];
              $jifeng = $jifeng1 ;
		      $data=array(
		        'userid' => $tempdata['user_id'],
				'user_name' => $username,
				'role_id' => $tempdata['role_id'],
				'type' => $jiajian,
				'name' => '由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作',
				'intro' => '由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作',
				'integral' => $jifeng,
				'time' => time(),
				'cao_username' => $GLOBALS['SCup']->SCup_user_msg['data']['user_name'],
		      );
			  if($jiajian ==1)
			  {
			     $data['totalintegral'] = $totaljifeng+$jifeng;
		         $TB_integral->integraladd($jifeng,$tempdata['user_id']);				 
				 
			  }else if($jiajian ==0)
			  {			  
			     $data['totalintegral'] = $totaljifeng-$jifeng;
		         $TB_integral->integralpay($jifeng,$tempdata['user_id']);					 
			  }		
		      $TB_integral->post($data);  
	          $notice = &get_message(array('msg'=>'操作成功','url'=>'?app=integral','title'=>'Notice'));
	          $notice ->display();			  
		 
		 }else if ($type == 2)
		 {
		 
              if($huiyuantype==1)
			  {   
		          $sql='select user_id,user_name,role_id,integral from '.DB_PREFIX.'users  where 1';
				  $db = & get_db();
		          $result = $db->get_all($sql);
                  foreach($result as $key=>$value)
				  {
			         $totaljifeng =$result['integral'];
                     $jifeng = $jifeng2 ;
		             $data=array(
		                'userid' => $value['user_id'],
				        'user_name' => $value['user_name'],
				        'role_id' => $value['role_id'],
				        'type' => 1,
				        'name' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'intro' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'integral' => $jifeng,
						'totalintegral' => $totaljifeng+$jifeng,
				        'time' => time(),
						'cao_username' => $GLOBALS['SCup']->SCup_user_msg['data']['user_name'],
		              );					  
		              $TB_integral->integraladd($jifeng,$value['user_id']);	
					  $TB_integral->post($data); 				  					 				  
				  }	
		           				  
				  			  
			  }else if($huiyuantype==2)
			  {
		          $sql='select user_id,user_name,role_id,integral from '.DB_PREFIX.'users  where 1';
				  $db = & get_db();
		          $result = $db->get_all($sql);
                  foreach($result as $key=>$value)
				  {
			         $totaljifeng =$result['integral'];
                     $jifeng = $jifeng2 ;
		             $data=array(
		                'userid' => $value['user_id'],
				        'user_name' => $value['user_name'],
				        'role_id' => $value['role_id'],
				        'type' => 0,
				        'name' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'intro' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'integral' => $jifeng,
						'totalintegral' => $totaljifeng+$jifeng,
				        'time' => time(),
						'cao_username' => $GLOBALS['SCup']->SCup_user_msg['data']['user_name'],
		              );					  
		              $TB_integral->integralpay($jifeng,$value['user_id']);	
					  $TB_integral->post($data); 				  					 				  
				  }	
				  			  
			  }else if($huiyuantype==3)
			  {
		          $sql='select user_id,user_name,role_id,integral from '.DB_PREFIX.'users  where role_id<>"'.ROLE_P_ID.'" and role_id<>"'.ROLE_V_ID.'"';
				  $db = & get_db();
		          $result = $db->get_all($sql);
                  foreach($result as $key=>$value)
				  {
			         $totaljifeng =$result['integral'];
                     $jifeng = $jifeng2 ;
		             $data=array(
		                'userid' => $value['user_id'],
				        'user_name' => $value['user_name'],
				        'role_id' => $value['role_id'],
				        'type' => 1,
				        'name' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'intro' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'integral' => $jifeng,
						'totalintegral' => $totaljifeng+$jifeng,
				        'time' => time(),
						'cao_username' => $GLOBALS['SCup']->SCup_user_msg['data']['user_name'],
		              );					  
		              $TB_integral->integraladd($jifeng,$value['user_id']);	
					  $TB_integral->post($data); 				  					 				  
				  }				  
			  }else if($huiyuantype==4)
			  {
		          $sql='select user_id,user_name,role_id,integral from '.DB_PREFIX.'users  where role_id<>"'.ROLE_P_ID.'" and role_id<>"'.ROLE_V_ID.'"';
				  $db = & get_db();
		          $result = $db->get_all($sql);
                  foreach($result as $key=>$value)
				  {
			         $totaljifeng =$result['integral'];
                     $jifeng = $jifeng2 ;
		             $data=array(
		                'userid' => $value['user_id'],
				        'user_name' => $value['user_name'],
				        'role_id' => $value['role_id'],
				        'type' => 0,
				        'name' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'intro' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'integral' => $jifeng,
						'totalintegral' => $totaljifeng+$jifeng,
				        'time' => time(),
						'cao_username' => $GLOBALS['SCup']->SCup_user_msg['data']['user_name'],
		              );					  
		              $TB_integral->integralpay($jifeng,$value['user_id']);	
					  $TB_integral->post($data); 				  					 				  
				  }				  
			  }else if($huiyuantype==5)
			  {
		          $sql='select user_id,user_name,role_id,integral from '.DB_PREFIX.'users  where role_id="'.ROLE_P_ID.'"';
				  $db = & get_db();
		          $result = $db->get_all($sql);
                  foreach($result as $key=>$value)
				  {
			         $totaljifeng =$result['integral'];
                     $jifeng = $jifeng2 ;
		             $data=array(
		                'userid' => $value['user_id'],
				        'user_name' => $value['user_name'],
				        'role_id' => $value['role_id'],
				        'type' => 1,
				        'name' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'intro' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'integral' => $jifeng,
						'totalintegral' => $totaljifeng+$jifeng,
				        'time' => time(),
						'cao_username' => $GLOBALS['SCup']->SCup_user_msg['data']['user_name'],
		              );					  
		              $TB_integral->integraladd($jifeng,$value['user_id']);	
					  $TB_integral->post($data); 				  					 				  
				  }				  
			  }else if($huiyuantype==6)
			  {
		          $sql='select user_id,user_name,role_id,integral from '.DB_PREFIX.'users  where role_id="'.ROLE_P_ID.'"';
				  $db = & get_db();
		          $result = $db->get_all($sql);
                  foreach($result as $key=>$value)
				  {
			         $totaljifeng =$result['integral'];
                     $jifeng = $jifeng2 ;
		             $data=array(
		                'userid' => $value['user_id'],
				        'user_name' => $value['user_name'],
				        'role_id' => $value['role_id'],
				        'type' => 0,
				        'name' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'intro' => $content.'(由管理员'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'操作)',
				        'integral' => $jifeng,
						'totalintegral' => $totaljifeng+$jifeng,
				        'time' => time(),
						'cao_username' => $GLOBALS['SCup']->SCup_user_msg['data']['user_name'],
		              );					  
		              $TB_integral->integralpay($jifeng,$value['user_id']);	
					  $TB_integral->post($data); 				  					 				  
				  }					  
			  }

	          $notice = &get_message(array('msg'=>'操作成功','url'=>'?app=integral','title'=>'Notice'));
	          $notice ->display();		     
		 
		 
		 }
         		 		 
		 $this->index();

    }



    function del()
    {

	    if(!isset($_POST['jobs'])){$_POST['jobs']=0;}	
		$jobs = $_POST['jobs'];
        if($jobs ==='del')
		 {
		    if(!isset($_POST['checkdata'])){$_POST['checkdata']=array();}
             if(empty($_POST['checkdata']))
			 {
		    $notice = &get_message(array('msg'=>"请至少选择一个删除项目！",'url'=>'?app='.$this->type.'&act=index','title'=>'Notice'));
		    $notice ->display();				 
			 }
		    $checkdata = $_POST['checkdata'];		
			$str='';
			foreach($checkdata as $key => $value)
			{
			  $str.="id='".$value."'  or ";		  
			}			
			$str = substr($str, 0,-3);
			$db = & get_db();
			$db->query("DELETE FROM ".DB_PREFIX."integral WHERE "	.$str. " ");	
		    $notice = &get_message(array('msg'=>"删除成功！",'url'=>'?app=integral&act=index','title'=>'Notice'));
		    $notice ->display();				 
	         
		}	
			 
		 $this->index();	  

    }

    function onetime()
    {

	    if(!isset($_POST['jobs'])){$_POST['jobs']=0;}	
		$jobs = $_POST['jobs'];
        if($jobs ==='onetime')
		 {	
		    $time = time();	  
            $end_time = $time-30*24*60*60;
	        $str = "time<$end_time";
			$db = & get_db();
			$db->query("DELETE FROM ".DB_PREFIX."integral WHERE "	.$str. " ");			
		    $notice = &get_message(array('msg'=>"删除成功！",'url'=>'?app=integral&act=index','title'=>'Notice'));
		    $notice ->display();				 
	         
		}	
			 
		 $this->index();	  

    }
	
    function toling()
    {

	    if(!isset($_POST['jobs'])){$_POST['jobs']=0;}	
		$jobs = $_POST['jobs'];
        if($jobs ==='toling')
		 {	
		    $TB_integral = &$this->_model('integral');		
		    $data = $TB_integral->integraltoling();						
		    $notice = &get_message(array('msg'=>"积分清零成功！",'url'=>'?app=integral&act=index','title'=>'Notice'));
		    $notice ->display();				 
	         
		}	
			 
		 $this->index();	  

    }
	
}

    function role_con($str)
    {
	      $array = array();
          $str_array = explode('|',$str);
	      foreach($str_array as $key => $value)
	      {
	            if($value)
		        {
			   
			   $array[$key] = $value;
		   
		        }
	 
	      }    
	   
	      return $array; 		

    }

?>